package cn.edu.ctbu.api01.controller;

import cn.edu.ctbu.api01.domain.Student;
import cn.edu.ctbu.api01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("list")
    public String list() {
        return "list"; // 返回模板名称
    }

    @RequestMapping("list3")
    public String list3(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        if(session.getAttribute("userid") == null) {
            //没有登录，需要去登录
            return "redirect:/login/user";
        }

        List<Student> student=studentService.findAll();
        model.addAttribute("data", student);
        // 注意：返回一个静态页面
        return "/student/list3"; // 正常返回模板名称，不要重定向
    }
}

